#include "ogl_buffer.h"
#include "ogl_graphics.h"

#include "util/timer.h"				// log
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    

extern "C"
{
    
#ifdef _SDL2
    
#include "SDL2/include/SDL.h"
#ifdef WIN32
#include "glee/glee.h"
    //#include "SDL-1.2.13/include/SDL_opengl.h"
#else
#ifdef _OPENGLES2
#include "SDL2/include/SDL_opengles2.h"
#else
#include "SDL2/include/SDL_opengles.h"
#endif
#endif
    
#else
    
    #include "SDL-1.2.13/include/SDL.h"
    #ifdef WIN32
        #include "glee/glee.h"
        //#include "SDL-1.2.13/include/SDL_opengl.h"
    #else
        #include "SDL-1.2.13/include/SDL_opengles.h"
    #endif
    
#endif   // _SDL2
}

#include <cassert>



oglVertexBuffer::oglVertexBuffer(unsigned int Count, unsigned int StepSize, unsigned int FVF) :
	GraphicsBuffer(Count, StepSize),
	m_FVF(FVF),
	m_pBuffer(0)
{
	glGenBuffers(1, &m_pBuffer);
	glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer);
	glBufferData(GL_ARRAY_BUFFER, m_TotalSize, NULL, GL_DYNAMIC_DRAW);

}

oglVertexBuffer::~oglVertexBuffer()
{
	glDeleteTextures(1,&m_pBuffer);
}
void	oglVertexBuffer::Load(void* pSrc, int Count)
{
	if (!m_pBuffer) return;
	if (Count*m_StepSize > m_TotalSize) return;

	glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer);
	glBufferSubData(GL_ARRAY_BUFFER, 0,  m_StepSize * Count,   pSrc);
}
/////////////////////////////////////////////


oglIndexBuffer::oglIndexBuffer(unsigned int Count) :
			GraphicsBuffer(Count, sizeof(GLubyte)),
	m_pBuffer(0)
{
	
	GLubyte* Temp = new GLubyte[m_TotalSize];

  	for(int i =0; i < m_TotalSize; i++)
		Temp[i] = i; 

	glGenBuffers(1, &m_pBuffer);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffer);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_TotalSize, Temp, GL_STATIC_DRAW);

	delete Temp;
}

oglIndexBuffer::~oglIndexBuffer()
{
	glDeleteTextures(1,&m_pBuffer);

}
void	oglIndexBuffer::Load(void* pSrc, int Count)
{
	if (!m_pBuffer) return;
	if (Count*m_StepSize > m_TotalSize) return;
 
	glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer);
	glBufferSubData(GL_ARRAY_BUFFER, 0,  m_StepSize * Count,   pSrc);
}

